from pony import orm
from postgres.db_build import Hospital_coding, Hospital_mapping, HospitalBiecheng
import re


# 统一为英文符号
def ch_en(word: str) -> str:
    intab = u"！—。【】“”‘’《》—「」『』〖〗〔〕〓︰﹒﹕﹙﹚﹝﹞﹢〈〉∽∶‥–‒Ι×[]（）＃［］{}：％＊﹛﹜，－／÷＿﹡；ⅹ\\"
    outab = u"!-.()\"\"\'\'()-()()()()=:.:()()+()~::--i*()()#()():%*(),-//_*;*/"
    trantab = str.maketrans(intab, outab)  # 一一映射。
    word = word.translate(trantab)  # 返回字符串S的副本，其中每个字符都已通过给定的转换表进行映射。
    if re.findall("＜.*?＞", word):
        word = word.replace("＜", "(")
        word = word.replace("＞", ")")
    if re.findall("<.*?>", word):
        word = word.replace("<", "(")
        word = word.replace(">", ")")
    word = word.strip()
    return word


# 输入医院名称，匹配标准名称和编码
@orm.db_session()
def get_hospital_coding(hospital):
    hospital_id = ''
    name = hospital
    qs = orm.select(
        code
        for biecheng in HospitalBiecheng for code in Hospital_coding
        if
        biecheng.another_name == hospital
        and
        biecheng.name == code.hospital_name_nhsa
    )
    if qs.count():
        for item in qs:
            hospital_id = item.hospital_id
            name = item.hospital_name_nhsa
            break
    else:
        qs = orm.select(
            item
            for item in Hospital_mapping
            if
            item.hospital_name_bill == hospital
        )
        if qs.count():
            for item in qs:
                hospital_id = item.hospital_id
                name = item.hospital_name_nhsa
                break
    return (hospital_id, name)

# excel路径变超链接
def url_2_link(name, url):
    return f'=HYPERLINK("{url}", "{name}")'

if __name__ == '__main__':
    hospital_name = "广州市增城区新塘医院"
    print(get_hospital_coding(hospital_name))

